package com.yicong.ants.manager;

import com.alibaba.security.realidentity.RPEventListener;
import com.alibaba.security.realidentity.RPResult;
import com.alibaba.security.realidentity.RPVerify;
import com.cchao.simplelib.core.DevHelper;
import com.cchao.simplelib.core.RxBus;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseActivity;
import com.yicong.ants.Constants;
import com.yicong.ants.bean.account.RpBean;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 实人认证 helper
 *
 * @author cch
 * @version 2020/8/28
 */
public class RpHelper {

    public static RpBean rpBean;

    public static void getRpTokenStart(BaseActivity baseView, Map<String, String> map, RPEventListener listener) {
        baseView.showProgress();
        baseView.addSubscribe(RetrofitHelper.getApis().getRPToken(map)
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    baseView.hideProgress();
                    if (respBean.isCodeFail()) {
                        baseView.showToast(respBean.getMsg());
                        return;
                    }
                    rpBean = respBean.getData();
                    RPVerify.start(baseView, respBean.getData().getVerifyToken(), listener);
                }, RxHelper.getHideProgressConsumer(baseView)));
    }

    /**
     * 人脸认证（无需身份证，信息）
     */
    public static void startVerify(BaseActivity baseActivity) {
        startVerify(baseActivity, null);
    }

    public static void startVerify(BaseActivity baseActivity, CallBack callBack) {
        RpHelper.getRpTokenStart(baseActivity, Collections.emptyMap(), new RPEventListener() {
            @Override
            public void onFinish(RPResult auditResult, String code, String msg) {
                if (DevHelper.isDev()) {
                    UiHelper.showToast("getRpTokenStart code " + code + msg);
                }
                if (auditResult == RPResult.AUDIT_PASS) {
                    // 认证通过。建议接入方调用实人认证服务端接口DescribeVerifyResult来获取最终的认证状态，并以此为准进行业务上的判断和处理。
                    authSuc(baseActivity, callBack);
                } else if (auditResult == RPResult.AUDIT_FAIL) {
                    // 认证不通过。建议接入方调用实人认证服务端接口DescribeVerifyResult来获取最终的认证状态，并以此为准进行业务上的判断和处理。
                    UiHelper.showConfirmDialog(baseActivity, "认证不通过", null);
                } else if (auditResult == RPResult.AUDIT_NOT) {
                    if (code.equals("3208")) {
                        UiHelper.showConfirmDialog(baseActivity, msg, null);
                        return;
                    }
                    // 未认证，具体原因可通过code来区分（code取值参见错误码说明），通常是用户主动退出或者姓名身份证号实名校验不匹配等原因，导致未完成认证流程。
                    UiHelper.showConfirmDialog(baseActivity, "未认证", null);
                }
            }
        });
    }

    static void authSuc(BaseActivity baseActivity, CallBack callBack) {
        baseActivity.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("BizId", RpHelper.rpBean.getBizId());
        baseActivity.addSubscribe(RetrofitHelper.getApis().auth(map)
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    baseActivity.hideProgress();
                    UiHelper.showLongToast(respBean.getMsg());
                    if (respBean.isCodeFail()) {
                        return;
                    }
                    UserManager.updateUserBean(respBean.getData());
                    if (callBack == null) {
                        RxBus.get().postEvent(Constants.Event.Auth_Suc);
                    } else {
                        callBack.callBack("");
                    }
                }, RxHelper.getHideProgressConsumer(baseActivity)));
    }

    /**
     * 普通业务流程 人脸识别
     *
     * @param sucCall call
     */
    public static void onRpCheckSuc(BaseActivity baseActivity, Runnable sucCall) {
        baseActivity.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("BizId", RpHelper.rpBean.getBizId());
        baseActivity.addSubscribe(RetrofitHelper.getApis().rpCheckSuc(map)
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    baseActivity.hideProgress();
                    UiHelper.showLongToast(respBean.getMsg());
                    if (respBean.isCodeFail()) {
                        return;
                    }
                    sucCall.run();
                }, RxHelper.getHideProgressConsumer(baseActivity)));
    }
}
